package net.sf.rose.jdbc.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import net.sf.rose.jdbc.bean.annotation.Column;
import net.sf.rose.jdbc.bean.annotation.Table;
import net.sf.rose.jdbc.bean.annotation.Type;

/**
 * 类说明：数据权限定义表
 * 
 * @author 作者 fgg047 E-mail: 9110530@qq.com
 * @version 创建时间：2021-07-11
 */
@Table(name = "ADMIN_DATA_AUTHOR", description = "数据权限定义表")
public class DataAuthorBean {

	@Column(name = "DATA_ID", pk = true, type = Type.定长文本, description = "编号", canNull = false, size = 32, policy = "UUID")
	private String dataID = null;

	@Column(name = "MENU_ID", type = Type.变长文本, description = "菜单编号", canNull = false, size = 32)
	private String menuID = null;

	@Column(name = "FIELD_NAME", type = Type.变长文本, description = "字段名称", canNull = false, size = 50)
	private String fieldName = null;

	/** 1：授权；2：不授权；3：默认授权 */
	@Column(name = "AUTHOR_USER", type = Type.数字整型, description = "授权作者")
	private int authorUser = 1;

	/** 1：授权；2：不授予；3：默认授权 */
	@Column(name = "AUTHOR_LEADER", type = Type.数字整型, description = "授权部门领导")
	private int authorLeader = 1;

	/** 1：启用；2：禁用 */
	@Column(name = "USE_AUTHOR", type = Type.数字整型, description = "启用授权")
	private int useAuthor = 2;

	/** 页面所有角色授权的角色 */
	private Map<String, DataAuthorDetail> authorRoleMap = new HashMap<>();

	/** 页面所有用户授权的角色 */
	private Map<String, DataAuthorDetail> authorUserMap = new HashMap<>();

	/** 明细表 */
	private DataAuthorDetail authorDetail = null;

	public List<String> getAuthorValueByRoles(String roles) {
		List<String> list = new ArrayList<>();
		if (authorRoleMap.size() == 0) {
			return list;
		}
		if (roles.indexOf(',') != -1) {
			String[] array = StringUtils.split(roles, ',');
			for (String str : array) {
				DataAuthorDetail author = authorRoleMap.get(str);
				if (author != null) {
					String value = author.getAuthorValue();
					if (value.indexOf(',') != -1) {
						for (String s : value.split(",")) {
							list.add(s);
						}
					} else {
						list.add(value);
					}
				}
			}
		} else {
			DataAuthorDetail author = authorRoleMap.get(roles);
			if (author != null) {
				String value = author.getAuthorValue();
				if (value.indexOf(',') != -1) {
					for (String s : value.split(",")) {
						list.add(s);
					}
				} else {
					list.add(value);
				}
			}
		}
		if (list.size() > 0) {
			String[] array = new String[list.size()];
			for (int i = 0; i < list.size(); i++) {
				array[i] = list.get(i);
			}
			list = this.format(array);
		}
		return list;
	}

	public List<String> getAuthorValueUserID(String userID) {
		List<String> list = new ArrayList<>();
		if (authorUserMap.size() == 0) {
			return list;
		}
		DataAuthorDetail author = authorUserMap.get(userID);
		if (author != null) {
			String value = author.getAuthorValue();
			if (value.indexOf(',') != -1) {
				for (String s : value.split(",")) {
					list.add(s);
				}
			} else {
				list.add(value);
			}
		}
		if (list.size() > 0) {
			String[] array = new String[list.size()];
			for (int i = 0; i < list.size(); i++) {
				array[i] = list.get(i);
			}
			list = this.format(array);
		}
		return list;
	}

	public String getDataID() {
		return dataID;
	}

	public void setDataID(String dataID) {
		this.dataID = dataID;
	}

	public String getMenuID() {
		return menuID;
	}

	public void setMenuID(String menuID) {
		this.menuID = menuID;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public Map<String, DataAuthorDetail> getAuthorRoleMap() {
		return authorRoleMap;
	}

	public void setAuthorRoleMap(Map<String, DataAuthorDetail> authorRoleMap) {
		this.authorRoleMap = authorRoleMap;
	}

	public int getAuthorLeader() {
		return authorLeader;
	}

	public void setAuthorLeader(int authorLeader) {
		this.authorLeader = authorLeader;
	}

	public int getAuthorUser() {
		return authorUser;
	}

	public void setAuthorUser(int authorUser) {
		this.authorUser = authorUser;
	}

	public Map<String, DataAuthorDetail> getAuthorUserMap() {
		return authorUserMap;
	}

	public void setAuthorUserMap(Map<String, DataAuthorDetail> authorUserMap) {
		this.authorUserMap = authorUserMap;
	}

	/**
	 * 格式化数组，将被包含的数据项去掉
	 * 
	 * @param array
	 * @return
	 */
	private List<String> format(String[] array) {
		for (int i = 0; i < array.length; i++) {
			if (array[i] == null) {
				continue;
			}
			for (int j = 0; j < array.length; j++) {
				if (i == j || array[j] == null) {
					continue;
				}
				// 如果源包含目标，则源不需要
				if (array[i].startsWith(array[j])) {
					array[i] = null;
					break;
				}
				// 如果目标包含源，则目标不需要
				else if (array[j].startsWith(array[i])) {
					array[j] = null;
				}
			}
		}
		List<String> list = new ArrayList<>();
		for (String s : array) {
			if (s != null) {
				list.add(s);
			}
		}
		return list;
	}

	public static void main(String[] args) {
		String[] array = new String[] {
				"000100010001",
				"000100010002",
				"00010002",
				"0002",
				"00010001",
				"000100020001"
		};
		DataAuthorBean bean = new DataAuthorBean();
		List<String> list = bean.format(array);
		// list=00010001,00010002,0002
		System.out.println(list);
		StringBuilder sb = new StringBuilder();
		for (String s : list) {
			sb.append(s).append(',');
		}
		System.out.println(sb.substring(0, sb.length() - 1));
	}

	public DataAuthorDetail getAuthorDetail() {
		return authorDetail;
	}

	public void setAuthorDetail(DataAuthorDetail authorDetail) {
		this.authorDetail = authorDetail;
	}

	public int getUseAuthor() {
		return useAuthor;
	}

	public void setUseAuthor(int useAuthor) {
		this.useAuthor = useAuthor;
	}

}
